/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  SRVStateConnected.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "SRVStateConnected.h"
#import "SRVStateInitialised.h"
#import "SRVStateBlackfinUnavailable.h"
#import "SRVStateBlackfinAvailable.h"
#import "SRVInterthreadMessageStreamable.h"
#import "SRVGetVersionOperation.h"

// matchport connected
// a) drain incoming rubbish from previous disconnect (part of SRVIOStream on connection operation)
// b) ping with an SRVGetVersionOperation to see if the blackfin is available
// c) if BF responds and version ok then move to available otherwise move to unavailable.

@implementation SRVStateConnected

- (void) stateInitialise {
	[self pingBlackfin];
}

- (void) disconnect {
	// need to initate a disconnect
	[stateSwitcher setState:[SRVStateBlackfinUnavailable alloc]]; // not available
}

// set up a get version operation
- (void) pingBlackfin {
	opExpiryTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
														  target:self
														selector:@selector(getVersionOpExpired:)
														userInfo:nil
														 repeats:NO];
	
	SRVGetVersionOperation *getVersionOp = [[[SRVGetVersionOperation alloc] init] retain];
	[getVersionOp setCompletionListener:self];
	SRVInterthreadMessageStreamable *streamable = [[SRVInterthreadMessageStreamable alloc] init];
	[streamable setObject:getVersionOp];
	[stateSwitcher sendMessage:streamable];
}

- (void) operationCompleted:(SRVOperation *)operation {
	NSLog(@"operation complete - version");
	[opExpiryTimer invalidate];
	[stateSwitcher setState:[SRVStateBlackfinAvailable alloc]]; // available!	
}

- (void) operationFailed:(SRVOperation *)operation {
	[opExpiryTimer invalidate];
	[stateSwitcher setState:[SRVStateBlackfinUnavailable alloc]]; // not available!	
}

- (void) getVersionOpExpired:(NSTimer*)theTimer {
	NSLog(@"get version attempt expired");
	[stateSwitcher setState:[SRVStateBlackfinUnavailable alloc]];	// not available
}

- (void) handleChildMessage:(SRVInterthreadMessage *)msg {
	if( msg == nil ) {
		NSLog(@"received nil message!");
	}
	switch( [msg type] ) {
		case DISCONNECT: // disconnected
			NSLog(@"SRVBlackfinController User disconection completed response received.");
			[self disconnectComplete:self];
			break;
		case STREAMABLE: {
			NSLog(@"SRVBlackfinController streamable response received.");
			[self operationCompleted:nil];
		}
			break;
		default:
			NSLog(@"SRVBlackfinController received unknown interthread message @", msg);
			break;
	}
	
}

@end
